package com.sfh.cpc.home.menu;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ScrollView;

import com.sfh.cpc.R;
import com.sfh.cpc.com.ui.VipLimit;
import com.sfh.cpc.home.fragment.view.MassNewFragmentView;
import com.sfh.cpc.home.fragment.view.MassRecommendFragmentView;
import com.sfh.cpc.home.fragment.view.MassVipFragmentView;
import com.sfh.cpc.home.fragment.view.NoteHotFragmentView;
import com.sfh.cpc.home.fragment.view.NoteNewFragmentView;
import com.sfh.cpc.home.fragment.view.NoteRecommendFragmentView;
import com.sfh.cpc.home.fragment.view.NoteVipFragmentView;
import com.sfh.lib.ui.AbstractLifecycleFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述: 首页
 *
 * @author SunFeihu 孙飞虎
 * @date 2018/8/6
 */

public class HomeFragment extends AbstractLifecycleFragment {

    ScrollView scrollView;
    int size = 1;
    List<Fragment> list = new ArrayList<>(6);

    @Override
    public int getLayout() {
        return R.layout.home_fragment;
    }

    @Override
    public void initData(View view) {
        scrollView = view.findViewById(R.id.ly_scroll);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                @Override
                public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    View childView = scrollView.getChildAt(0);

                    if (childView.getMeasuredHeight() <= scrollView.getScrollY() + scrollView.getHeight()) {
                        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                        transaction.add(R.id.ly_content, getFragments());
                        transaction.commit();
                    }
                }
            });
        }else{
        }

        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        //推荐 话题 4
        transaction.add(R.id.ly_content, NoteRecommendFragmentView.newFragment(0));
        // 热门 话题 6
        transaction.add(R.id.ly_content, NoteHotFragmentView.newFragment(0));
        //最新 话题 6
        transaction.add(R.id.ly_content, NoteNewFragmentView.newFragment(0));
        //  推荐 社团 7
        transaction.add(R.id.ly_content, MassRecommendFragmentView.newFragment(0));
        //  最新 社团 4
        transaction.add(R.id.ly_content, MassNewFragmentView.newFragment(0));

        if (VipLimit.vip()){
            //VIP 话题 9
            transaction.add(R.id.ly_content, NoteVipFragmentView.newFragment(0));
            //VIP 社团 9
            transaction.add(R.id.ly_content, MassVipFragmentView.newFragment(0));
        }
        transaction.commit();

    }



    private Fragment getFragments() {
        if (list.isEmpty()) {
            list.add(NoteHotFragmentView.newFragment(size));
            list.add(NoteNewFragmentView.newFragment(size));
            list.add(MassRecommendFragmentView.newFragment(size));
            list.add(MassNewFragmentView.newFragment(size));
            if (VipLimit.vip()){
                list.add(NoteVipFragmentView.newFragment(size));
                list.add(MassVipFragmentView.newFragment(size));
            }
            size++;
        }
        return list.remove(0);
    }
}
